package com.geek.gkpush.framework.ds;

import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.support.TransactionTemplate;

/**
 * <h1>数据库配置类。</h1><br/>
 *
 * <h1><br/><a href="https://u.geekbang.org/subject/arch3/100614201">Java 业务架构实战营：https://u.geekbang.org/subject/arch3/100614201</a></h1><br/><h1><a href="https://gitee.com/ylimhhmily/BizArchPracticeCamp">Java 业务架构实战营代码地址：https://gitee.com/ylimhhmily/BizArchPracticeCamp</a></h1><br/><h1><a href="https://u.geekbang.org/subject/java4th/1001148?source=app_share">高级 Java 工程体系课 2023 版：https://u.geekbang.org/subject/java4th/1001148?source=app_share</a></h1><br/><h1><a href="https://time.geekbang.org/column/intro/100312101?tab=catalog">Dubbo 源码剖析与实战：https://time.geekbang.org/column/intro/100312101?tab=catalog</a></h1>
 *
 * @author hmilyylimh
 * ^_^
 * @version 0.0.1
 * ^_^
 * @date 2023/10/1 05:41
 */
@SuppressWarnings("ALL")
@Configuration
@MapperScan(DsConsts.SCAN_MAPPER_INTERFACE_PKG)
public class DsConfig {

    @Lazy(false)
    @Bean(name = "databaseSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Autowired DruidDataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource);

        sqlSessionFactory.setVfs(SpringBootVFS.class);

        String configLocation = DsConsts.MYBATIS_CONFIG_FILE_NAME;
        sqlSessionFactory.setConfigLocation(new PathMatchingResourcePatternResolver().getResource(configLocation));

        String mapperLocations = DsConsts.MAPPER_XML_LOCATIONS;
        sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));

        String typeAliasesPackage = DsConsts.SCAN_MAPPER_MODEL_TYPE_ALIASES_PKG;
        sqlSessionFactory.setTypeAliasesPackage(typeAliasesPackage);

//        sqlSessionFactory.setPlugins(new Interceptor[]{
//                pageInterceptor()
//        });

        return sqlSessionFactory.getObject();
    }

    @Bean(name = "databaseSqlSession")
    public SqlSessionTemplate sqlSessionTemplate(@Autowired @Qualifier("databaseSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory);
        return template;
    }

    @Bean(name = "transactionManager")
    public DataSourceTransactionManager dataSourceTransactionManager(@Autowired DruidDataSource dataSource) {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource);
        return transactionManager;
    }

    @Bean(name = "transactionTemplate")
    public TransactionTemplate transactionTemplate(@Autowired DataSourceTransactionManager dataSourceTransactionManager) {
        TransactionTemplate template = new TransactionTemplate();
        template.setTransactionManager(dataSourceTransactionManager);
        return template;
    }

//    @Bean
//    public Interceptor pageInterceptor() {
//        PageInterceptor interceptor = new PageInterceptor();
////        interceptor.setProperties("dialect", "");
//        return interceptor;
//    }
}